<?php

namespace app\proxy\model\plus\coupon;

use app\common\model\plus\coupon\Coupon as CouponModel;
use app\proxy\model\supplier\Supplier;
use app\proxy\model\auth\User as ProxyUser;

class Coupon extends CouponModel
{

    public function getList($data, $regionIds)
    {
        $model = $this;
        if (isset($data['shop_supplier_id'])) {
            $model = $model->where('coupon.shop_supplier_id', '=', $data['shop_supplier_id']);
        }
        return $model->alias('coupon')->field(['coupon.*','supplier.name as supplier_name'])
            ->join('supplier', 'coupon.shop_supplier_id = supplier.shop_supplier_id', 'left')
            ->where('coupon.is_delete', '=', 0)
            ->where(function ($query) use ($regionIds) {
                $query->whereIn('coupon.county_id', $regionIds)->whereOr("coupon.province_id", "in", $regionIds)->whereOr("coupon.city_id", "in", $regionIds);
            })
            ->order(['coupon.sort' => 'asc', 'coupon.create_time' => 'desc'])
            ->paginate($data);
    }

    /**
     * 添加新记录
     */
    public function add($data, $proxyId=0)
    {
        $data['app_id'] = self::$app_id;
        $data['shop_supplier_id'] = 0;
        if ($data['expire_type'] == '20') {
            $data['start_time'] = strtotime($data['active_time'][0]);
            $data['end_time'] = strtotime($data['active_time'][1]);
        }
        // 限制商品id
        if ($data['apply_range'] == 20) {
            $data['product_ids'] = implode(',', $data['product_ids']);
        }
        $category_first_ids = [];
        $category_second_ids = [];
        if ($data['apply_range'] == 30) {
            if (isset($data['category_list']['first'])) {
                foreach ($data['category_list']['first'] as $item) {
                    array_push($category_first_ids, $item['category_id']);
                }
            }
            if (isset($data['category_list']['second'])) {
                foreach ($data['category_list']['second'] as $item) {
                    array_push($category_second_ids, $item['category_id']);
                }
            }
            $data['category_ids'] = [
                'first' => $category_first_ids,
                'second' => $category_second_ids
            ];
            $data['category_ids'] = json_encode($data['category_ids']);
        }
        if ($data["supplier_range"] == 1) {
            if (empty($data["region_id"][0])) {
                $this->error = "请指定商户地区";
                return false;
            }
            $data["shop_supplier_id"] =0;
            $data["province_id"] = $data["region_id"][0];
            $data["city_id"] = $data["region_id"][1] ?? 0;
            $data["county_id"] = $data["region_id"][2] ?? 0;
        } elseif ($data["supplier_range"] == 2) {
            $shop_supplier_id = $data["supplier_exclude_ids"][0] ?? 0;
            if (empty($shop_supplier_id)) {
                $this->error = "请指定商户";
                return false;
            }
            $data["shop_supplier_id"] = $shop_supplier_id;
            $supplier = Supplier::detail($shop_supplier_id);
            if (empty($supplier) || $supplier["is_audit"] != 1 || $supplier["is_delete"] == 1 || $supplier["status"] != 0) {
                $this->error = "指定商户不存在或已禁用22:{$supplier["is_delete"]}";
                return false;
            }
            $data["province_id"] = $supplier["province_id"] ?: 0;
            $data["city_id"] = $supplier["city_id"] ?: 0;
            $data["county_id"] = $supplier["county_id"] ?: 0;
        }
        //免审
        $nosh = ProxyUser::checkauth($proxyId,'coupon');
        $data["is_audit"] = $nosh?1:0;
        return self::create($data);
    }

    public function edit($data, $proxyId=0)
    {
        if ($data['expire_type'] == '20') {
            $data['start_time'] = strtotime($data['active_time'][0]);
            $data['end_time'] = strtotime($data['active_time'][1]);
        }
        // 限制商品id
        if($data['apply_range'] == 20){
            $data['product_ids'] = implode(',', $data['product_ids']);
        }
        $category_first_ids = [];
        $category_second_ids = [];
        if($data['apply_range'] == 30){
            if(isset($data['category_list']['first'])){
                foreach($data['category_list']['first'] as $item){
                    array_push($category_first_ids, $item['category_id']);
                }
            }
            if(isset($data['category_list']['second'])) {
                foreach ($data['category_list']['second'] as $item) {
                    array_push($category_second_ids, $item['category_id']);
                }
            }
            $data['category_ids'] = [
                'first' => $category_first_ids,
                'second' => $category_second_ids
            ];
            $data['category_ids'] = json_encode($data['category_ids']);
        }
        if ($data["supplier_range"] == 1) {
            if (empty($data["region_id"][0])) {
                $this->error = "请指定商户地区";
                return false;
            }
            $data["province_id"] = $data["region_id"][0];
            $data["city_id"] = $data["region_id"][1] ?? 0;
            $data["county_id"] = $data["region_id"][2] ?? 0;
            $data["shop_supplier_id"] =0;
        } elseif ($data["supplier_range"] == 2) {
            $shop_supplier_id = $data["supplier_exclude_ids"][0] ?? 0;
            if (empty($shop_supplier_id)) {
                $this->error = "请指定商户";
                return false;
            }
            $data["shop_supplier_id"] = $shop_supplier_id;
            $supplier = Supplier::detail($shop_supplier_id);
            if (empty($supplier) || $supplier["is_audit"] != 1 || $supplier["is_delete"] == 1 || $supplier["status"] != 0) {
                $this->error = "指定商户不存在或已禁用";
                return false;
            }
            $data["province_id"] = $supplier["province_id"] ?: 0;
            $data["city_id"] = $supplier["city_id"] ?: 0;
            $data["county_id"] = $supplier["county_id"] ?: 0;
        }
        $where['coupon_id'] = $data['coupon_id'];

        //免审
        $nosh = ProxyUser::checkauth($proxyId,'coupon');
        $data["is_audit"] = $nosh?1:0;
        unset($data['coupon_id']);
        return self::update($data, $where);
    }

    public function setDelete(array $where)
    {
        return self::update(['is_delete' => 1], $where);
    }
}